<?php
/**
 * 商品分类
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class GoodsCategory extends Model
{
    //set talbename
    protected $table = 'goods_category';
    //set primary key
    protected $pk = 'cat_id';

    protected $field = ["cat_id", "cat_name", "en_name", "keywords", "cat_desc", "img", "parent_id", "sort_order", "is_show", "is_hot", "create_time", "update_time", "pid", "status"];


    /**
     * @desc 增加商品分类
     * @param $category_name
     * @param $keywords
     * @param $cat_desc
     * @return false|int
     */
    public function addGoodsCategory($category_name, $keywords, $cat_desc, $parent_id, $img)
    {
        $param['cat_name'] = $category_name;
        $param['keywords'] = $keywords;
        $param['cat_desc'] = $cat_desc;
        $param['parent_id'] = $parent_id;
        $param['img'] = $img;
        $param['create_time'] = time();
        $param['update_time'] = time();
        $this->data($param);
        return $this->save();
    }

    /**
     * @desc 编辑商品分类
     * @param $param
     * @return false|int
     */
    public function editGoodsCategory($param)
    {
        $conds = array(
            'cat_id' => $param['cat_id'],
        );
        $field = array();
        foreach ($param as $key => $value) {
            if ($key == 'cat_id') {
                continue;
            }
            $field[$key] = $value;
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 获取分类
     * @param $cat_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsCategory($cat_id)
    {
        $conds = array(
            'cat_id' => $cat_id,
            'status' => 0,
        );
        $field = 'cat_id, cat_name, cat_desc, keywords, img';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * @desc 获取分类
     * @param $cat_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getSubGoodsCategory($parent_id)
    {
        $conds = array(
            'parent_id' => $parent_id,
            'status' => 0,
        );
        $field = 'cat_id, cat_name, cat_desc, keywords, img';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * @desc 获取所有商品分类
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAllGoodsCategory()
    {
        $field = 'cat_id, parent_id, cat_name, cat_desc, keywords, img';
        return Db::table($this->table)->field($field)->where('status', 0)->select();
    }

    /**
     * @param $cat_id
     * @return int
     * @throws \think\Exception
     */
    public function deleteGoodsCategory($cat_id)
    {
        $conds = [
            'cat_id' => $cat_id,
        ];

        $field = [
            'status' => 1,
        ];
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @param array $param
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsCateGoryByConds($param = [])
    {
        $arrConds = [];
        foreach ($param as $key => $value) {
            if (in_array($key, $this->field)) {
                $arrConds[$key] = $value;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return $this->where($arrConds)->select();
    }
}
